﻿using Kylin.Wiki.Core.Utilities;
using Kylin.Wiki.IService;
using Kylin.Wiki.Model.Api;
using Kylin.Wiki.Model.Dto;
using Kylin.Wiki.Model.View;
using Kylin.Wiki.Web.Common;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;

namespace Kylin.Wiki.Web.Controllers.Document
{
    [Route("api/[controller]")]
    [ApiController]
    public class DocumentController : BaseController
    {
        private readonly IDocumentService _documentService;
         

        public DocumentController(IDocumentService documentService)
        {
            _documentService = documentService; 
        }

        /// <summary>
        /// 根据用户获取Wiki集合
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetList"), Description("根据用户获取Wiki集合")]
        public ApiResult<List<DocumentVM>> GetList()
        {
            ApiResult<List<DocumentVM>> result = new ApiResult<List<DocumentVM>>();
            result.Data = _documentService.GetList(UserContext.Current.UserId);
            return result;
        }

        [HttpGet, Description("根据ID获取Wiki")]
        public ApiResult<DocumentViewVM> Get(int wikiID,int userId, [FromServices] IWebHostEnvironment environment)
        {
            return _documentService.Get(wikiID,userId, environment.WebRootPath);
        }

        [HttpPost, Description("创建Wiki")]
        public ApiResult<bool> Create([FromBody] DocumentDTO documentDto, [FromServices] IWebHostEnvironment environment)
        {
            documentDto.UserId = UserContext.Current.UserId;
            documentDto.CreateBy = UserContext.Current.UserName;
            documentDto.CreateOn = DateTime.Now;
            documentDto.UpdateBy = UserContext.Current.UserName;
            documentDto.UpdateOn = DateTime.Now;

            return _documentService.Create(documentDto, environment.WebRootPath);
        }

        [HttpPut, Description("更新Wiki")]
        public ApiResult<bool> Update([FromBody] DocumentDTO documentDTO, [FromServices] IWebHostEnvironment environment)
        {
            documentDTO.UpdateBy = UserContext.Current.UserName;
            documentDTO.UpdateOn = DateTime.Now;
            return _documentService.Update(documentDTO, environment.WebRootPath);
        }

        [HttpDelete("{id}")]
        public ApiResult<bool> Delete(int id, [FromServices] IWebHostEnvironment environment)
        {
            ApiResult<bool> result = new ApiResult<bool>();
            if (_documentService.Exists(x => x.Path.Contains(id.ToString())))
                return new ApiResult<bool>(false, Core.Enum.HttpStateCode.Error, "该Wiki有子项，请先删除子项");
            var document = _documentService.FindFirst(x => x.ID == id);
            _documentService.Delete(document, true);
            var pid = document.Path.Split(',');
            var pDocumentNames = string.Join('/', _documentService.Find(x => pid.Contains(x.ID.ToString())).Select(x => x.DocumentName).ToList());
            string filePath = $"/{pDocumentNames}/{document.DocumentName}";
            filePath = Path.Combine(environment.WebRootPath, "Files/" + filePath);
            //删除旧的文件夹下的文件 
            FileHelper.DeleteFolder(filePath);

            return new ApiResult<bool>(true, Core.Enum.HttpStateCode.Success, "删除成功");
        }

        

    }
}
